Repository navigation
Check enum values against their own enum in typed catches and isOfType - #492
Merged
Merged
Conversation
A generated enum has no class id of its own. Its class inherits EnumBase_obj's _hx_ClassId, clsIdEnum, and every enum value answers true to that id. hx::ObjectPtr's cast (CastPtr, in hx/Object.h) and hx::TCanCast (hx/Class.h) both test the id, so in compiled code a typed catch of one enum, which Haxe 4 generates as _hx_e.IsClass< ::A >(), caught a value of any enum, Std.isOfType(v, A), and so v is A, was true for any enum value through the TCanCast<A_obj> the enum registers with, and cast(v, A) returned a value of another enum instead of throwing. cppia asked the same TCanCast when the type was a host enum, so the interpreter and the JIT were wrong in the same way for host enums. HX_DO_ENUM_RTTI, which Haxe expands in every generated enum, now gives the enum _hx_isEnumValueOf, which compares the value's class with the enum's own. CastPtr and TCanCast go through hx::IsInstanceOfType, which uses _hx_isEnumValueOf when the type has it and the class id otherwise. Classes, hx::EnumBase and Enum keep the check they had. Both are one virtual call and a comparison. EnumInstanceChecks covers typed catches, Std.isOfType, is and safe casts with plain enums, enums with parameters and type parameters, a value held as Dynamic and haxe.Exception or Dynamic after an enum catch. CppiaEnumInstanceChecks checks host and script enums both ways, compiled, interpreted and jitted. The expected output of both is what Haxe's interpreter prints. The new benchmarks time Std.isOfType and typed catches of enums, and typed catches of classes. Fixes #491 HaxeFoundation/haxe#12151
The helper the cppia JIT calls to build a host enum value with arguments read them from the script frame in place, and wrote the new value back the same way. A frame keeps 4-byte alignment, so on 64-bit an object slot in it can sit at an odd multiple of 4: undefined behaviour, and a fault on a CPU that requires alignment. 7a78380 moved the JIT's helpers to copying slots in and out and missed this one. CppiaEnumInstanceChecks is the first test to build a host enum with an argument from jitted code at such an offset, HostB.HostWith(1), and UBSan stopped it in createEnum (CppiaClasses.cpp:368, load of misaligned address). The helper now uses JitArg and JitStore, as the others do.
dimensionscape
force-pushed
the
fix/enum-instance-checks
branch
from
November 27, 2026 15:47
a37515e to
e19aa70
Compare
dimensionscape
marked this pull request as ready for review
November 27, 2026 15:48
This was referenced Nov 27, 2026
dimensionscape
added a commit
that referenced
this pull request
Nov 28, 2026
#492 gave HX_DO_ENUM_RTTI _hx_isEnumValueOf, which compares a value's class with the enum's own __mClass, and returned false when __mClass is null. An extern enum written by hand in C++, as Haxe's unit.issues.Issue10830 does, never registers its class, so every value of it failed its own check: the switch in that test threw Invalid Cast, and so would a typed catch of it or a cast to it. 6.0.2 shipped with it, since the haxe unit job runs only on pushes to main. An enum whose __mClass is null takes any enum value again, through the clsIdEnum check every enum used before #492. Enums Haxe generates have their class, and still check for their own. EnumUnregisteredClass is Issue10830's extern enum, with a switch, a cast from Dynamic and a typed catch, and checks that generated enums still tell each other apart. It threw Invalid Cast before this change. Tests: EnumUnregisteredClass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #491. For 6.0.2: rebased onto main at f059754, with its
Changes.mdentries under main'slibhxcpp 6.0.2 (unreleased)heading. The third commit fixes what UBSan reported in this pull request's first run, described under "The UBSan report" below.What was wrong
In compiled code a typed catch of one enum took a value of any enum, and
Std.isOfType,isand a safe cast agreed with it:Haxe's interpreter gives the
Bbranch,false, and a throw. cppia had the same fault where the type was a host enum: a script's catch orisOfTypeagainst a host enum took any enum value, a script's own included. Script enums were already right.Cause
The class Haxe 4.3 generates for an enum declares no
_hx_ClassIdof its own. It inheritsEnumBase_obj's,clsIdEnum, andEnumBase_obj::_hx_isInstanceOfanswers true for that id on every enum value. A typed catch (IsClass<T>), a safe cast (TCast) andStd.isOfType(TCanCast<T>, through__instanceof) all end in that test, and so does cppia for a host enum. Haxe 5's generator gives each enum its own id (HaxeFoundation/haxe#12151, fixed ondevelopment); 4.3's will not change, so the runtime has to answer.The change
HX_DO_ENUM_RTTIgives every generated enum_hx_isEnumValueOf, which compares the value's class with the enum's own.hx::IsInstanceOfType<T>ininclude/hx/Object.huses that where the type has it and the class id otherwise.CastPtrandTCanCastcall it.hx::EnumBaseandEnumkeep their old check, and the code generated for class types is unchanged. Enums generated by Haxe 5 take the new path and get the same answer as before.This changes what compiled programs observe: a value of another enum now passes a typed catch,
isOfTypeandisanswer false for it, and a safe cast of it throws.Std.isOfType(v, Enum)andType.getEnumanswer as before, and a value is still an instance of its own enum, type parameters or not. Code built against the old headers, such as a prebuilt static library, keeps the old answer until it is rebuilt.The UBSan report
The first CI run here passed the core suites on Linux, Windows and macOS, ASan and TSan, and failed UBSan on the new
CppiaEnumInstanceChecks:That is not the enum check.
createEnumis the helper the cppia JIT calls to build a host enum value with arguments. It read them from the script frame in place and wrote the new value back the same way, and a frame keeps 4-byte alignment, so on 64-bit an object slot can sit at an odd multiple of 4. 7a78380 moved the JIT's helpers to copying slots in and out and did not touch this file. The new test is the first to reach the helper at such an offset, withHostB.HostWith(1)in jitted code.The third commit makes
createEnumuseJitArgandJitStore, as the other helpers do, and has its ownChanges.mdline.There is no UBSan on the machine this was written on, so the UBSan job on this push is that fix's test. What I could run: with a temporary print in
createEnum, the test reaches it once, with the frame at 4 modulo 8 and one argument, and with the fix the test's output is unchanged, interpreted and jitted.Tests
test/regression/EnumInstanceChecks: typed catches in both orders and alone,isOfType,is, safe casts, enums with parameters and with type parameters, a value held asDynamic, and a catch ofhaxe.Exception, a class orDynamicafter an enum catch. Its 66 lines equal what the interpreter prints.test/regression/CppiaEnumInstanceChecks: script and host enums both ways, interpreted and jitted. Script first, so it needs no line inemscripten-skip.txt.IsOfTypeClassEnumpasses unchanged.Run on 64-bit Windows with MSVC and Haxe 4.3.7: the two above,
IsOfTypeClassEnum,EnumFormattingShapes,CppiaSafeCasts,CppiaBoolBoxing, the cppia suite interpreted and with-jit(56 assertions each), andtest/haxe(2698 assertions).After the rebase, and again with the third commit: the two new tests,
IsOfTypeClassEnum, main's newFloatFromSingle, and the cppia suite interpreted and with-jit(56 assertions each).Not run here: the whole of
test/regression,test/std, the sanitizers, 32-bit and the other platforms.Cost
For an enum type the check was one virtual call and an integer compare, and is now one virtual call (
__GetClass) and a pointer compare.bench/src/EnumBenchmarks.hxgains three benchmarks. Against main, withbench/ci.sh(5 processes of 10 samples):enum_is_of_typeenum_typed_catchclass_typed_catchNothing moved past the spread. The two enum benchmarks check the count of checks and throws, not the answers, since main answers them wrongly and stops at the first catch clause.
Nothing in libhxcpp's own code leaned on the old answer: no
isOfTypeorisnames an enum type, and the only typed enum catches are ofhaxe.io.Errorin two socket tests, where only that enum arrives.